﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.IO;

namespace CoolCode.Web.Mvc 
{
	public static class TextWriterExtensions
	{
		public static void Partial(this TextWriter writer, ViewContext viewContext, string partialViewName)
		{
			var view = ViewEngines.Engines.TryLocatePartial(viewContext, partialViewName);
			var newViewData = new ViewDataDictionary();
			var newContext = new ViewContext(viewContext, viewContext.View, newViewData, viewContext.TempData, writer);
			view.Render(newContext, writer);
		}

		public static void Partial<T>(this TextWriter writer, ViewContext viewContext, string partialViewName, T item)
		{
			var view = ViewEngines.Engines.TryLocatePartial(viewContext, partialViewName);
			var newViewData = new ViewDataDictionary<T>(item);
			var newContext = new ViewContext(viewContext, viewContext.View, newViewData, viewContext.TempData, writer);
			view.Render(newContext, writer);
		}
	}
}
